<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User extends MX_Controller
{

    private $module = 'user';
    private $table = 'user';

    function __construct()
    {
        parent::__construct();
        $this->load->model($this->module . '_model', 'model');
        $this->load->model('admincp_modules/admincp_modules_model');
        if ($this->uri->segment(1) == 'admincp') {
            if ($this->uri->segment(2) != 'login') {
                if (!$this->session->userdata('userInfo')) {
                    header('Location: ' . PATH_URL . 'admincp/login');
                    return false;
                }
                $get_module = $this->admincp_modules_model->check_modules($this->uri->segment(2));
                $this->session->set_userdata('ID_Module', $get_module[0]->id);
                $this->session->set_userdata('Name_Module', $get_module[0]->name);
            }
            $this->template->set_template('admin');
            $this->template->write('title', 'Admin Control Panel');
        }
    }

    /*------------------------------------ Admin Control Panel ------------------------------------*
    }
    /*------------------------------------ End Admin Control Panel --------------------------------*/
    function index()
    {
        $data = '';
        $this->template->write_view('content', 'FRONTEND/index', $data);
        $this->template->write('meta_keywords', META_KEY);
        $this->template->write('meta_description', META_DESC);
        $this->template->write('title', 'Danh sách thành viên | ' . SITE_NAME);
        $this->template->render();
    }

    function login()
    {
        $data = '';
        if ($this->_is_logged_in()) {
            redirect('user/' . $this->session->userdata('username'));
        }
        if ($_POST) {
            //Data
            $user_email = $this->input->post('email', true);
            $password = $this->input->post('pass', true);
            $userdata = $this->model->validate($user_email, md5($password . SECRET_KEY));

            //Validation
            if ($userdata) {
                if ($userdata->status == 0) {
                    $data['error'] = "Thành viên bị khóa!";
                } else {
                    $data['userid'] = $userdata->id;
                    $data['username'] = $userdata->username;
                    $data['logged_in'] = true;
                    $this->session->set_userdata($data);
                    redirect(PATH_URL . 'user/' . $userdata->username);
                }
            } else {
                $data['error'] = "Mật khẩu không đúng!";
            }

            return;
        }
        $this->template->write_view('content', 'FRONTEND/login', $data);
        $this->template->write('meta_keywords', META_KEY);
        $this->template->write('meta_description', 'Đăng nhập: ' . META_DESC);
        $this->template->write('title', 'Đăng nhập | ' . SITE_NAME);
        $this->template->render();
    }

    function register()
    {
        $err = '';
        if (!empty($_POST)) {
            $data['nicename'] = $this->input->post('fullname');
            $data['email'] = $this->input->post('email');
            $data['password'] = md5($this->input->post('pass') . SECRET_KEY);
            $data['username'] = $this->input->post('name');
            if ($this->model->checkExit($this->input->post('email'), 'email')) {
                $err = 'Email đã tồn tại!';
            } elseif ($this->model->checkExit($this->input->post('name'), 'username')) {
                $err = 'Tên đăng nhập đã tồn tại!';
            } else {
                $create = $this->model->create($data);
                if ($create) {
                    $err = 'Đăng ký thành công!';
                    redirect('user/register-success');
                } else {
                    $err = 'Đăng ký không thành công!';
                }
            }
        }
        $data['err'] = $err;
        $this->template->write_view('content', 'FRONTEND/register', $data);
        $this->template->write('meta_keywords', META_KEY);
        $this->template->write('meta_description', 'Đăng ký: ' . META_DESC);
        $this->template->write('title', 'Đăng ký | ' . SITE_NAME);
        $this->template->render();
    }

    function register_success()
    {
        $data = '';
        $this->template->write_view('content', 'FRONTEND/register_success', $data);
        $this->template->write('meta_keywords', META_KEY);
        $this->template->write('meta_description', 'Đăng ký: ' . META_DESC);
        $this->template->write('title', 'Đăng ký | ' . SITE_NAME);
        $this->template->render();

    }

    function member($nicename)
    {
        $data["user"] = $this->model->user_by_nicename($nicename);
        if ($data["user"]) {
            $this->template->write_view('content', 'FRONTEND/register_success', $data);
            $this->template->write('meta_keywords', META_KEY);
            $this->template->write('meta_description', 'Đăng ký: ' . META_DESC);
            $this->template->write('title', $nicename . ' | ' . SITE_NAME);
            $this->template->render();
        } else {
            show_404();
        }
    }

    function post()
    {
        if (!$this->_is_logged_in()) {
            //redirect('user/' . 'login');
        }
        $data["user"] = $this->model->user_by_nicename($this->session->userdata('username'));
        if ($data["user"]) {
            $this->template->write_view('content', 'FRONTEND/post', $data);
            $this->template->write('meta_keywords', META_KEY);
            $this->template->write('meta_description', 'Thành viên đăng : ' . META_DESC);
            $this->template->write('title', 'Thành viên đăng bài | ' . SITE_NAME);
            $this->template->render();
        } else {
            show_404();
        }
    }

    /*------------------------------------ FRONTEND ------------------------------------*/

    function userdata()
    {
        if ($this->_is_logged_in()) {
            return $this->model->user_by_id($this->session->userdata('userid'));
        } else {
            return false;
        }
    }

    function logout()
    {
        $this->session->sess_destroy();
        redirect('');
    }

    function _is_admin()
    {
        if (@$this->users->userdata()->role === 1) {
            return true;
        } else {
            return false;
        }
    }

    function _member_area()
    {
        if (!$this->_is_logged_in()) {
            redirect('user/login');
        }
    }

    function _is_logged_in()
    {
        if ($this->session->userdata('logged_in')) {
            return true;
        } else {
            return false;
        }
    }
    /*------------------------------------ End FRONTEND --------------------------------*/
}